package com.weimob.mcloud.wechatwork.customer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.base.SingleRequest;
import com.weimob.mcloud.wechatwork.core.base.SingleResponse;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.Tag;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagGroup;
import com.weimob.mcloud.wechatwork.core.service.tag.CustomerTagCoreService;
import com.weimob.mcloud.wechatwork.core.service.tag.TagCoreService;
import com.weimob.mcloud.wechatwork.core.service.tag.request.SingleTagRequest;
import com.weimob.mcloud.wechatwork.customer.service.TagBaseService;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2022/3/22 3:01 下午
 */
@Slf4j
@Service
public class TagBaseServiceImpl implements TagBaseService {

    @Reference
    private TagCoreService tagCoreService;

    @Reference
    private CustomerTagCoreService customerTagCoreService;

    @Override
    public Boolean batchInsertTagGroup(List<TagGroup> tagGroupList, String corpId) {
        if (CollectionUtils.isEmpty(tagGroupList) || StringUtils.isBlank(corpId)) {
            return false;
        }

        for (TagGroup tagGroup : tagGroupList) {
            if (StringUtils.isBlank(tagGroup.getCorpId()) || StringUtils.isBlank(tagGroup.getExternalTagGroupId())
                    || StringUtils.isBlank(tagGroup.getTagGroupName())) {
                return false;
            }
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleRequest<List<TagGroup>> request = new SingleRequest<>();
        request.setPrivateSphere(privateSphere);
        request.setData(tagGroupList);

        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = tagCoreService.batchInsertTagGroup(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("添加标签组失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> response = soaResponse.getResponseVo();
        if (null == response || null == response.getData()) {
            log.error("添加标签组失败, 返回结果为空, request:{}", JSON.toJSONString(request));
            return false;
        }

        return response.getData();
    }

    @Override
    public Boolean batchUpdateTagGroup(List<TagGroup> tagGroupList, String corpId) {
        if (StringUtils.isBlank(corpId) || CollectionUtils.isEmpty(tagGroupList)) {
            return false;
        }

        for (TagGroup tagGroup : tagGroupList) {
            if (StringUtils.isBlank(tagGroup.getCorpId()) || StringUtils.isBlank(tagGroup.getExternalTagGroupId())
                    || StringUtils.isBlank(tagGroup.getTagGroupName())) {
                return false;
            }
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleRequest<List<TagGroup>> request = new SingleRequest<>();
        request.setPrivateSphere(privateSphere);
        request.setData(tagGroupList);

        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = tagCoreService.batchUpdateTagGroup(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("编辑标签组失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> response = soaResponse.getResponseVo();
        if (null == response || null == response.getData()) {
            log.error("编辑标签组失败, 返回结果为空, request:{}", JSON.toJSONString(request));
            return false;
        }

        return response.getData();
    }

    @Override
    public Boolean batchInsertTag(List<Tag> tagList, String corpId) {
        if (CollectionUtils.isEmpty(tagList) || StringUtils.isBlank(corpId)) {
            return false;
        }

        for (Tag tag : tagList) {
            if (StringUtils.isBlank(tag.getCorpId()) || StringUtils.isBlank(tag.getExternalTagId())
                    || StringUtils.isBlank(tag.getTagName())) {
                return false;
            }

            TagGroup tagGroup = tag.getTagGroup();
            if (StringUtils.isBlank(tagGroup.getCorpId()) || StringUtils.isBlank(tagGroup.getExternalTagGroupId())) {
                return false;
            }
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleRequest<List<Tag>> request = new SingleRequest<>();
        request.setPrivateSphere(privateSphere);
        request.setData(tagList);

        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = tagCoreService.batchInsertTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("添加标签失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> response = soaResponse.getResponseVo();
        if (null == response || null == response.getData()) {
            log.error("添加标签失败, 返回结果为空, request:{}", JSON.toJSONString(request));
            return false;
        }

        return response.getData();
    }

    @Override
    public Boolean batchUpdateTag(List<Tag> tagList, String corpId) {
        if (CollectionUtils.isEmpty(tagList) || StringUtils.isBlank(corpId)) {
            return false;
        }

        for (Tag tag : tagList) {
            if (StringUtils.isBlank(tag.getCorpId()) || StringUtils.isBlank(tag.getExternalTagId())
                    || StringUtils.isBlank(tag.getTagName())) {
                return false;
            }
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleRequest<List<Tag>> request = new SingleRequest<>();
        request.setPrivateSphere(privateSphere);
        request.setData(tagList);

        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = tagCoreService.batchUpdateTag(request);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("编辑标签失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> response = soaResponse.getResponseVo();
        if (null == response || null == response.getData()) {
            log.error("编辑标签失败, 返回结果为空, request:{}", JSON.toJSONString(request));
            return false;
        }

        return response.getData();
    }


    @Override
    public List<TagGroup> queryGroupByGroupIds(String corpId, List<String> tagGroupIds) {
        if (StringUtils.isBlank(corpId) || CollectionUtils.isEmpty(tagGroupIds)) {
            return new ArrayList<>();
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleTagRequest<TagGroup> singleTagRequest = new SingleTagRequest<>();
        singleTagRequest.setTagGroupIdList(tagGroupIds);
        singleTagRequest.setPrivateSphere(privateSphere);
        singleTagRequest.setDeleted(false);
        SoaResponse<SingleResponse<List<TagGroup>>, Void> soaResponse = tagCoreService.fetchTagGroup(singleTagRequest);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("查询标签组失败, request:{}, error:{}", JSON.toJSONString(singleTagRequest), soaResponse.getReturnMsg());
            return new ArrayList<>();
        }

        SingleResponse<List<TagGroup>> responseVo = soaResponse.getResponseVo();
        if (null == responseVo) {
            log.warn("查询标签组不存在, request:{}", JSON.toJSONString(singleTagRequest));
            return new ArrayList<>();
        }

        return responseVo.getData();
    }

    @Override
    public List<TagGroup> queryGroupByCorpId(String corpId) {
        if (StringUtils.isBlank(corpId)) {
            return new ArrayList<>();
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleTagRequest<TagGroup> singleTagRequest = new SingleTagRequest<>();
        singleTagRequest.setPrivateSphere(privateSphere);
        singleTagRequest.setDeleted(false);

        SoaResponse<SingleResponse<List<TagGroup>>, Void> soaResponse = tagCoreService.fetchTagGroup(singleTagRequest);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("根据corpId查询所有标签组失败, corpId:{}, error:{}", corpId, soaResponse.getReturnMsg());
            return new ArrayList<>();
        }

        SingleResponse<List<TagGroup>> tagGroupResp = soaResponse.getResponseVo();
        if (null == tagGroupResp) {
            log.warn("查询标签组失败, corpId:{}", corpId);
            return new ArrayList<>();
        }

        return tagGroupResp.getData();
    }

    @Override
    public List<Tag> queryTagByGroupId(String corpId, String groupId) {
        if (StringUtils.isBlank(corpId) || StringUtils.isBlank(groupId)) {
            return new ArrayList<>();
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);


        SingleTagRequest<Tag> singleTagRequest = new SingleTagRequest<>();
        singleTagRequest.setPrivateSphere(privateSphere);
        singleTagRequest.setTagGroupIdList(Lists.newArrayList(groupId));
        singleTagRequest.setDeleted(false);
        SoaResponse<SingleResponse<List<Tag>>, Void> soaResponse = tagCoreService.fetchTag(singleTagRequest);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("根据标签组查询标签失败, request:{}, error:{}", JSON.toJSONString(singleTagRequest), soaResponse.getReturnMsg());
            return new ArrayList<>();
        }

        SingleResponse<List<Tag>> tagListResp = soaResponse.getResponseVo();
        if (null == tagListResp) {
            log.warn("查询标签不存在, request:{}", JSON.toJSONString(singleTagRequest));
            return new ArrayList<>();
        }

        return tagListResp.getData();
    }

    @Override
    public List<Tag> queryTagByTagIds(String corpId, List<String> tagIdList, Boolean needFullInfo) {
        if (StringUtils.isBlank(corpId) || CollectionUtils.isEmpty(tagIdList)) {
            return new ArrayList<>();
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleTagRequest<Tag> singleTagRequest = new SingleTagRequest<>();
        singleTagRequest.setPrivateSphere(privateSphere);
        singleTagRequest.setDeleted(false);
        singleTagRequest.setTagIdList(tagIdList);
        singleTagRequest.setNeedFullInfo(needFullInfo);
        SoaResponse<SingleResponse<List<Tag>>, Void> soaResponse = tagCoreService.fetchTag(singleTagRequest);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("根据标签id查询标签失败, request:{}, error:{}", JSON.toJSONString(singleTagRequest), soaResponse.getReturnMsg());
            return new ArrayList<>();
        }

        SingleResponse<List<Tag>> tagListResp = soaResponse.getResponseVo();
        if (null == tagListResp) {
            log.warn("根据标签id查询标签信息不存在, request:{}", JSON.toJSONString(singleTagRequest));
            return new ArrayList<>();
        }

        return tagListResp.getData();
    }

    @Override
    public Boolean deletedTagGroupByGroupIds(String corpId, List<String> tagGroupIds) {
        if (StringUtils.isBlank(corpId) || CollectionUtils.isEmpty(tagGroupIds)) {
            return false;
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleTagRequest<TagGroup> singleTagRequest = new SingleTagRequest<>();
        singleTagRequest.setPrivateSphere(privateSphere);
        singleTagRequest.setTagGroupIdList(tagGroupIds);
        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = tagCoreService.deletedTagGroup(singleTagRequest);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("删除标签组失败, request:{}, error:{}", JSON.toJSONString(singleTagRequest), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || null == responseVo.getData()) {
            log.warn("删除标签组失败, request:{}", JSON.toJSONString(singleTagRequest));
            return false;
        }

        return responseVo.getData();
    }

    @Override
    public Boolean deletedTagByTagIds(String corpId, List<String> tagIds) {
        if (StringUtils.isBlank(corpId) || CollectionUtils.isEmpty(tagIds)) {
            return false;
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);

        SingleTagRequest<Tag> singleTagRequest = new SingleTagRequest<>();
        singleTagRequest.setPrivateSphere(privateSphere);
        singleTagRequest.setTagIdList(tagIds);
        SoaResponse<SingleResponse<Boolean>, Void> soaResponse = tagCoreService.deletedTag(singleTagRequest);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("根据标签id删除标签失败, request:{}, error:{}", JSON.toJSONString(singleTagRequest), soaResponse.getReturnMsg());
            return false;
        }

        SingleResponse<Boolean> responseVo = soaResponse.getResponseVo();
        if (null == responseVo || null == responseVo.getData()) {
            log.warn("根据标签id删除标签失败, request:{}", JSON.toJSONString(singleTagRequest));
            return false;
        }

        return responseVo.getData();
    }
}
